package com.sdkx.app.datalayers;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;

import com.sdkx.app.R;
import com.sdkx.app.model.AlarmPoint;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import cn.creable.gridgis.display.Display;
import cn.creable.gridgis.display.FillSymbol;
import cn.creable.gridgis.display.LineSymbol;
import cn.creable.gridgis.geodatabase.Feature;
import cn.creable.gridgis.geodatabase.FeatureClass;
import cn.creable.gridgis.geodatabase.IFeature;
import cn.creable.gridgis.geometry.Arithmetic;
import cn.creable.gridgis.geometry.GeometryType;
import cn.creable.gridgis.geometry.LineString;
import cn.creable.gridgis.geometry.Point;
import cn.creable.gridgis.geometry.Polygon;
import cn.creable.gridgis.util.Image;

public class AlarmAreaLayer extends MapDraw implements LocationListener {
    Paint paint;
    List<AlarmItem> points  = new ArrayList<>();
    List<AlarmItem> flypoints  = new ArrayList<>();
    private FeatureClass fc;
    Image image;
    boolean alarmflag  =false;
    boolean isFlying  = false;
    long time =0;

    public AlarmAreaLayer(Context context) {
        super(context);
        paint = new Paint();
        paint.setTextSize(18);
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
        fc = new FeatureClass(new String[]{"text"}, GeometryType.Point);
        BitmapDrawable bitmapDrawable =(BitmapDrawable)appContext.getDrawable(R.drawable.gps);
        image = new Image(bitmapDrawable.getBitmap());
    }

    @Override
    protected void draw2(Canvas canvas, Display display) {
        float zoom  = display.getDisplayTransformation().getZoom();
        if(zoom <2.764279e-02f) {
            if (points.size() > 0) {
                Canvas oldG = display.getCanvas();
                display.setCanvas(canvas);
                LineSymbol symbol = new LineSymbol();
                Point scrPoint;// = new Point();
                for (AlarmItem item : points) {
                    symbol.setWidth(item.point.getLineWidth());
                    symbol.setColor(item.point.getLinecolor());
                    if(item.alarming) {
                        if(!alarmflag) {
                            display.DrawPolyline(item.lineString, symbol);
                        }else {
                            FillSymbol fillSymbol = new FillSymbol();
                            fillSymbol.setColor(item.point.lineColor);
                            display.DrawPolygon(new Polygon(item.lineString.toLinearRing()),fillSymbol);
                        }
                        if(System.currentTimeMillis() - time > 1000) {
                            alarmflag = !alarmflag;
                            time = System.currentTimeMillis();
                        }
                    }else{
                        display.DrawPolyline(item.lineString, symbol);
                    }
                    display.DrawImagePoint(new Point(item.point.getDoubleLongitude(),item.point.getDoubleLatitude()),image);
                }
                display.setCanvas(oldG);
                display.setLabelSize(20);
                display.setLabelColor(Color.RED);
                display.labelFeatureClass(canvas, fc, 0,32, 16);
            }

        }
    }

    @Override
    public void upData() {
        List<AlarmItem> templist = new ArrayList<>();
        List<AlarmPoint> ap = LitePal.findAll(AlarmPoint.class);
        Vector<IFeature> fts = new Vector<IFeature>();
        if(ap.size() > 0) {
            AlarmItem item;
            for (AlarmPoint point : ap) {
                if(isInScreen(point.getDoubleLongitude(),point.getDoubleLatitude())) {
                    item = new AlarmItem();
                    item.point = point;
                    item.lineString = getCircleLines(new Point(point.getDoubleLongitude(), point.getDoubleLatitude()), point.getRadius());
                    templist.add(item);
                    IFeature ft = new Feature(new Point(point.getDoubleLongitude(), point.getDoubleLatitude()), new String[]{point.getName()});
                    fts.addElement(ft);
                }
            }
        }
        if(!isFlying)
            points = templist;
        else{
            flypoints = templist;
        }
        fc.setFeatures(fts);
    }

    @Override
    public void onLocationChanged(Location location) {
        if(isFlying) {
            if(flypoints.size() == 0)return;
            ArrayList<AlarmItem> items = new ArrayList<>();
            for (AlarmItem item : flypoints) {
                if (checkDistance(location, item.point)) {
                    items.add(item);
                }
            }
            points = items;
        }
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    private class AlarmItem {
       public  AlarmPoint point;
       public  LineString lineString;
       public  boolean alarming;
    }

    private boolean checkDistance(Location location,AlarmPoint point){
       double dis = Arithmetic.getDistance(location.getLongitude(),location.getLatitude(),point.getDoubleLongitude(),point.getDoubleLatitude());
       return (dis/1000 < 20);
    }

    public void setFlaying(boolean flag){
        isFlying = flag;
    }

}
